package 单表查询;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 单表查询
 */

public class Demo01 {
    public static void main(String[] args) throws Exception{
        //1、加载驱动com.mysql.jdbc.Driver
        Class.forName("com.mysql.jdbc.Driver");
        //2、使用驱动管理器（DriverManager）连接数据库
        String url = "jdbc:mysql://localhost:3306/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
        String user = "root";
        String pass = "000000";
        Connection conn = DriverManager.getConnection(url, user, pass);
        //3、书写SQL语句
        String sql = "SELECT * from t_user";
        //4、创建执行SQL的对象
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        //5、从rs中取出结果
        while (rs.next()){
            String username = rs.getString("username");
            String fullname = rs.getString("fullname");
            System.out.println(username+"，"+fullname);
        }
        //6、关闭连接
        rs.close();
        stmt.close();
        conn.close();
    }
}
//DriverManager管理驱动
//Connection连接数据库:获取执行SQL语句；管理事务
//Statement执行SQL语句
//ResultSet存放结果